//app.js
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },
  getUserInfo:function(cb){
    var that = this
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function (res) {
          let loginRes = res;

          // 获取用户信息
          wx.getUserInfo({
            success: function (info) {
              // console.log(info)
              that.globalData.userInfo = info.userInfo
              
              //获取openid接口（用户唯一标识）
              wx.request({
                url: 'https://api.weixin.qq.com/sns/jscode2session',
                data: {
                  appid: "wx30e7c37b64e7ed13",
                  secret: "9f1ba438dc13d82ace6075ec920b5df3",
                  js_code: loginRes.code,
                  grant_type: 'authorization_code'
                },
                method: 'GET',
                success: function (openid) {
                  // console.log(openid)
                  // 合并userInfo和openid
                  that.globalData.userInfo = Object.assign(that.globalData.userInfo, openid.data)
                  // that.setData({
                  //   openid: res.data.openid.substr(0, 10) + '********' + res.data.openid.substr(res.data.openid.length - 8, res.data.openid.length),
                  //   session_key: res.data.session_key.substr(0, 8) + '********' + res.data.session_key.substr(res.data.session_key.length - 6, res.data.session_key.length)
                  // })

                  typeof cb == "function" && cb(that.globalData.userInfo)
                }
              })
            }
          })
        }
      })
    }
  },
  globalData:{
    userInfo:null
  }
})